package com.jianzh5.message;

import org.apache.log4j.Logger;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.xml.sax.InputSource;

import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ResourceBundle;

public class SmsUtil {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(SmsUtil.class);

	private static final ResourceBundle bundle = ResourceBundle.getBundle("smsconf");
	
	public static final String AC = bundle.getString("sms.ac");
	public static final String AUTHOR = bundle.getString("sms.authkey");
	public static final String CSID = bundle.getString("sms.csid");
	public static final String CGID = bundle.getString("sms.cgid");
	public static final String SMSURL = bundle.getString("sms.smsurl");
	
	/**
	 * 发送手机短信获取返回文本信息
	 * @param phone	手机号码
	 * @param content 短信内容
	 * @return
	 */
	public static String sendOfReturnMessage(String phone,String content){
		return getReturn(sendOfReturnCode(phone,content));
	}

	/**
	 * 发送手机短信获取返回Code信息
	 * @param phone
	 * @param content
	 * @return
	 */
	public static Integer sendOfReturnCode(String phone, String content) {
		String src = "";
		try {
			src = "action=sendOnce" + "&ac=" + AC + "&authkey=" + AUTHOR + "&csid=" + CSID + "&cgid=" + CGID + "&m="
					+ phone + "&c="
					+ URLEncoder.encode(content, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			logger.info("激活短信转化失败,请检查数据....");
		}
		return getResultCode(SmsRequest.sendGet(SMSURL, src));
	}
	
	/**
	 * 根据短信状态码
	 * @param status
	 * @return
	 */
	private static String getReturn(Integer status) {
		String returns = "";
		switch (status) {
		
		case 1:
			returns = "发送成功";
			break;
			
		case 0:
			returns = "帐户格式不正确(正确的格式为:员工编号@企业编号)";
			break;
			
		case -1:
			returns = "服务器拒绝(速度过快、限时或绑定IP不对等)如遇速度过快可延时再发";
			break;
		case -2:
			returns = "密钥不正确";
			break;
		case -3:
			returns = "密钥已锁定";
			break;
		case -4:
			returns = "参数不正确(内容和号码不能为空，手机号码数过多，发送时间错误等)";
			break;
		case -5:
			returns = "无此帐户";
			break;
		case -6:
			returns = "帐户已锁定或已过期";
			break;
		case -7:
			returns = "帐户未开启接口发送";
			break;
		case -8:
			returns = "不可使用该通道组";
			break;
		case -9:
			returns = "帐户余额不足";
			break;
		case -10:
			returns = "内部错误";
			break;
		case -11:
			returns = "扣费失败";
			break;
		default:
			returns = "未知状态";
			break;
		}
		return returns;
	}
	
	/**
	 * 解析短信发送XML数据流返回结果
	 * @param SmsReturnXml
	 * @return
	 */
	private static Integer getResultCode(String SmsReturnXml) {
		Integer code = -32;
		//创建一个新的字符串
		StringReader read = new StringReader(SmsReturnXml);
		// 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
		InputSource source = new InputSource(read);
		// 创建一个新的SAXBuilder
		SAXBuilder sb = new SAXBuilder();
		try{
			Document doc = sb.build(source); // 取的根元素
			Element root = doc.getRootElement();
			code=Integer.parseInt(root.getAttributeValue("result"));
		}catch (Exception e) {
			e.printStackTrace();
		}
		return code;
	}

}
